package com.wz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.wz.entity.*;
import com.wz.service.impl.*;
import com.wz.util.JFreeChartUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class UserController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private DepServiceImpl depService;
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private EmpServiceImpl empService;

    @Autowired
    private RootServiceImpl rootService;

    @Autowired
    private EmpageServiceImpl empageService;

    @RequestMapping("/index")
    public String index(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model, HttpServletRequest request) {
        if (request.getSession().getAttribute("user") != null) {
            PageHelper.startPage(pageNum, 13);
            List<Emp> emps = empService.findEmp();
            PageInfo<Emp> pageInfo = new PageInfo<>(emps);//使用PageInfo包装类
            request.getSession().setAttribute("pageInfo", pageInfo);
            return "index";
        }
        model.addAttribute("msg", "非法操作！请进行登录！");
        return "login";
    }

    @RequestMapping("/user_show")
    public String usershow(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageNum1", defaultValue = "1") Integer pageNum1, Model model, HttpServletRequest request) {
        if (request.getSession().getAttribute("user") != null) {
            PageHelper.startPage(pageNum, 6);
            List<Dep> deps = depService.findDep();
            PageInfo<Dep> pageInfo1 = new PageInfo<>(deps);//使用PageInfo包装类
            request.getSession().setAttribute("pageInfo1", pageInfo1);

            PageHelper.startPage(pageNum1, 13);
            List<Empage> empages = empageService.findEmpage();
            PageInfo<Empage> pageInfo2 = new PageInfo<>(empages);//使用PageInfo包装类
            request.getSession().setAttribute("pageInfo2", pageInfo2);
            return "user_show";
        }
        model.addAttribute("msg", "非法操作！请进行登录！");
        return "login";
    }

    @RequestMapping("/login.html")
    public String userLogin(HttpServletRequest request) {
        request.getSession().invalidate();
        return "login";
    }

    @RequestMapping("/register.html")
    public String register() {
        return "register";
    }

    @RequestMapping("/seletEmp.html")
    public String toselectEmp(Model model) {

        if (request.getSession().getAttribute("root") != null) {
            return "seletEmp";
        }
        model.addAttribute("msg", "非法操作！请进行登录！");
        return "login";
    }

    @RequestMapping("/manger")
    public String tomanger(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model, HttpServletRequest request) {


        if (request.getSession().getAttribute("root") != null) {
            PageHelper.startPage(pageNum, 10);
            List<Emp> emps = empService.findEmp();
            PageInfo<Emp> pageInfo = new PageInfo<>(emps);//使用PageInfo包装类
            request.getSession().setAttribute("pageInfo", pageInfo);

            return "manger";
        }
        model.addAttribute("msg", "非法操作！请进行登录！");
        return "login";


    }

    @RequestMapping("/showdep")
    public String showdep(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageNum1", defaultValue = "1") Integer pageNum1, Model model, HttpServletRequest request) {
        if (request.getSession().getAttribute("root") != null) {
            PageHelper.startPage(pageNum, 4);
            List<Dep> deps = depService.findDep();
            PageInfo<Dep> pageInfo1 = new PageInfo<>(deps);//使用PageInfo包装类
            request.getSession().setAttribute("pageInfo1", pageInfo1);

            PageHelper.startPage(pageNum1, 3);
            List<Empage> empages = empageService.findEmpage();
            PageInfo<Empage> pageInfo2 = new PageInfo<>(empages);//使用PageInfo包装类
            request.getSession().setAttribute("pageInfo2", pageInfo2);
            return "showdep";
        }
        model.addAttribute("msg", "非法操作！请进行登录！");
        return "login";
    }

    @RequestMapping("/showuser")
    public String showuser(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model, HttpServletRequest request) {
        if (request.getSession().getAttribute("root") != null) {
            PageHelper.startPage(pageNum, 10);
            List<User> users = userService.findUsers();
            PageInfo<User> pageInfo3 = new PageInfo<>(users);//使用PageInfo包装类

            request.getSession().setAttribute("pageInfo3", pageInfo3);
            return "showuser";
        }
        model.addAttribute("msg", "非法操作！请进行登录！");
        return "login";
    }


    @RequestMapping("/login")
    public String login(String username, String password, String chose, Model model, HttpServletRequest request) {
        System.out.println(chose.equals("1"));
        if (chose.equals("1")) {
            root root = rootService.getRoot(username, password);
            if (root != null) {
                HttpSession httpSession = request.getSession();
                httpSession.setAttribute("root", root);
                PageHelper.startPage(1, 10);
                List<Emp> emps = empService.findEmp();
                PageInfo<Emp> pageInfo = new PageInfo<>(emps);//使用PageInfo包装类

                PageHelper.startPage(1, 4);
                List<Dep> deps = depService.findDep();
                model.addAttribute("deps", deps);
                PageInfo<Dep> pageInfo1 = new PageInfo<>(deps);//使用PageInfo包装类

                PageHelper.startPage(1, 3);
                List<Empage> empages = empageService.findEmpage();
                PageInfo<Empage> pageInfo2 = new PageInfo<>(empages);//使用PageInfo包装类

                PageHelper.startPage(1, 10);
                List<User> users = userService.findUsers();
                PageInfo<User> pageInfo3 = new PageInfo<>(users);//使用PageInfo包装类

                model.addAttribute("pageInfo", pageInfo);
                request.getSession().setAttribute("root", root);
                request.getSession().setAttribute("pageInfo", pageInfo);
                request.getSession().setAttribute("pageInfo1", pageInfo1);
                request.getSession().setAttribute("pageInfo2", pageInfo2);
                request.getSession().setAttribute("pageInfo3", pageInfo3);
                return "manger";
            } else {
                model.addAttribute("msg", "该管理员不存在");
                System.out.println("登录失败！");
                return "login";
            }


        } else {
            User user1 = userService.getUser(username, password);
            if (user1 != null) {

                request.getSession().setAttribute("user", user1);
//                model.addAttribute("user",user1);
//                model.addAttribute("emps",emps);
                PageHelper.startPage(1, 13);
                List<Emp> emps = empService.findEmp();
                PageInfo<Emp> pageInfo = new PageInfo<>(emps);//使用PageInfo包装类
                request.getSession().setAttribute("pageInfo", pageInfo);

                return "index";
            } else {
                model.addAttribute("msg", "该用户不存在");
                System.out.println("登录失败！");
                return "login";
            }


        }
    }
    @ResponseBody
    @RequestMapping("/upload")
    public  Map<String ,String>  upload(@RequestParam("avatar") MultipartFile avatar) {
        System.out.println(avatar);
        Map<String,String> result=new HashMap<>();
        String fileName = avatar.getOriginalFilename();
        fileName = UUID.randomUUID() + "_" + fileName;
        String dirPath = "G:\\Springboot大作业\\springboot-project-master\\springboot-anli\\src\\main\\resources\\static\\picture\\";
        File filePath = new File(dirPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        try {
            avatar.transferTo(new File(dirPath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
       result.put("avatar","/picture/" + fileName);
        System.out.println(fileName);
        return result;
    }
        @RequestMapping("/regist")
    public String register(User user, @RequestParam("avatarRoad")String  avatarRoad, Model model, HttpServletRequest request) {
            System.out.println(avatarRoad);
            List<User> users=userService.findUsers();
            int Te=0;
            for(User user1:users){
                if(user1.getUsername().equals(user.getUsername())){
                    Te=1;
                }
            }
            if(Te==0) {
                   User  saveUser=new User(user.getUsername(), user.getPassword(), avatarRoad);
                    userService.save(saveUser);
                    List<Emp> emps = empService.findEmp();
                    request.getSession().setAttribute("user", saveUser);
                    PageHelper.startPage(1, 13);
                    PageInfo<Emp> pageInfo = new PageInfo<>(emps);//使用PageInfo包装类
                    request.getSession().setAttribute("pageInfo", pageInfo);
                    return "index";
                }

            else {
                model.addAttribute("msg","账号已被注册！");
                return "register";
            }



    }

    @RequestMapping("/showTable")
    private String table_show(Model model) {
        if (request.getSession().getAttribute("user") != null) {
            List<Dep> deps = depService.findDep();
            List<Emp> emps = empService.findEmp();

            Map<String, Double> map = new HashMap<>();
            for (Dep dep : deps) {
                map.put(dep.getDepname(), Double.parseDouble(dep.getEmpnum()));
            }
            JFreeChartUtil.createPiePort("部门人数情况图", map, "G:\\Springboot大作业\\springboot-project-master\\springboot-anli\\src\\main\\resources\\static\\table\\bing.png");

            List<Empage> empages = empageService.findEmpage();
            Map<String, Double> map1 = new HashMap<>();
            for (Empage empage : empages) {
                map1.put(empage.getEname(), Double.parseDouble(empage.getAge()));
            }
            JFreeChartUtil.createLinePort("员工年龄情况图", map1, "", "员工年龄", "G:\\Springboot大作业\\springboot-project-master\\springboot-anli\\src\\main\\resources\\static\\table\\xian.png");

            Map<String, Double> map2 = new HashMap<>();
            for (Empage empage : empages) {
                map2.put(empage.getEname(), Double.parseDouble(empage.getYear()));
            }
            JFreeChartUtil.createBarPort("员工工龄龄情况图", map2, "G:\\Springboot大作业\\springboot-project-master\\springboot-anli\\src\\main\\resources\\static\\table\\zhu.png");
            Integer max = Integer.parseInt(empages.get(0).getAge());
            String Agename = empages.get(0).getEname();
            for (int i = 0; i < empages.size(); i++) {
                if (Integer.parseInt(empages.get(i).getAge()) > max) {
                    max = Integer.parseInt(empages.get(i).getAge());
                    Agename = empages.get(i).getEname();
                }
            }
            model.addAttribute("maxAge", max);
            model.addAttribute("maxAgeman", Agename);

            Integer max1 = Integer.parseInt(empages.get(0).getYear());
            String Yearname = empages.get(0).getEname();
            for (int i = 0; i < empages.size(); i++) {
                if (Integer.parseInt(empages.get(i).getYear()) > max1) {
                    max1 = Integer.parseInt(empages.get(i).getYear());
                    Yearname = empages.get(i).getEname();
                }
            }
            model.addAttribute("maxYear", max1);
            model.addAttribute("maxYearman", Yearname);
            Double sum = 0.0;
            for (int i = 0; i < emps.size(); i++) {
                sum += Double.parseDouble(emps.get(i).getSal());
            }
            Double mid = sum / emps.size();
            DecimalFormat df = new DecimalFormat("#.00");
            model.addAttribute("midSal", df.format(mid));
            return "showTable";
        }
        model.addAttribute("msg", "非法操作！请进行登录！");
        return "login";
    }





}






